package ru.susu.algebra.jtex;

import java.util.List;

import com.google.common.collect.Lists;

/**
 * @author akargapolov
 * @since: 16.08.2010
 */
public class TexDocument implements ITexDocument
{
	private List<ITexElement> _elements = Lists.newArrayList();
	private ITexSubItemsElement _bibliography;
	private ITexElement _head;

	@Override
	public ITexDocument setBibliography(ITexSubItemsElement bibliography)
	{
		_bibliography = bibliography;
		return this;
	}

	@Override
	public ITexDocument addElement(ITexElement element)
	{
		if (element != null)
			_elements.add(element);
		return this;
	}

	@Override
	public String generateContent()
	{
		UnionTexElement union = new UnionTexElement();
		TexBeginEndElement document = TexBeginEndElement.document();
		if (_head != null)
			union.addSubElement(_head);
		for (ITexElement element : _elements)
			document.addSubElement(element);
		if (_bibliography != null)
			document.addSubElement(_bibliography);
		union.addSubElement(document);
		return union.generateContent();
	}

	@Override
	public ITexDocument setHead(ITexElement head)
	{
		_head = head;
		return this;
	}

	@Override
	public ITexDocument newLine()
	{
		_elements.add(StringTexElement.text("\n\n"));
		return this;
	}

}
